using System;
using System.Web.Mvc;
using FrontEnd.Application.MembershipFacade;
using System.Diagnostics.Contracts;
using FrontEnd.Models;

namespace FrontEnd.Controllers
{
    [HandleError]
    public class MembershipController : Controller
    {
		public MembershipController(IMembershipFacade membership)
        {
			CodeContract.Requires(membership != null);
			this.Membership = membership;
        }

		public IMembershipFacade Membership
        {
            get;
            private set;
        }

        public ViewResult Register()
        {
            return this.View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(string email, string displayName, string password)
        {
			RegistrationResult result = this.Membership.Register(email, displayName, password);
            if (result.IsSuccess)
            {
				return this.RedirectToAction("RegistrationSuccess", new { PersonName = result.Person.UniqueName });   
            }
            else
            {
                this.ModelState.AddModelError("RegistrationFailed", "Registration failed..");
            }

            return View();
        }

        public ViewResult RegistrationSuccess(string accountId)
        {
            return this.View();
        }
		
        public ViewResult Login()
		{
			return this.View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(string email, string password, bool? remember, string returnUrl)
        {
			AuthenticationResult result = this.Membership.Authenticate(email, password);

            if (result.IsSuccess)
            {
                // Redirect to returnUrl
                if (String.IsNullOrEmpty(returnUrl) == false)
                {
                    return this.Redirect(returnUrl);
                }

                throw new NotImplementedException();
            }
            else
            {
                // Try again
            }

            throw new NotImplementedException();
        }
    }
}
